package com.wshl.cloudservice;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;


import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.lzy.okgo.OkGo;
import com.tencent.bugly.crashreport.CrashReport;
import com.wshl.cloudservice.agora.EngineEventListener;
import com.wshl.cloudservice.agora.Config;
import com.wshl.cloudservice.agora.Global;
import com.wshl.cloudservice.agora.IEventListener;
import com.wshl.cloudservice.utils.FileUtil;

import io.agora.rtc.Constants;
import io.agora.rtc.RtcEngine;
import io.agora.rtm.ErrorInfo;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmCallManager;
import io.agora.rtm.RtmClient;

public class CloudServiceApplication extends Application {
    private static final String TAG = CloudServiceApplication.class.getSimpleName();

    private RtcEngine mRtcEngine;
    private RtmClient mRtmClient;
    private RtmCallManager rtmCallManager;
    private EngineEventListener mEventListener;
    private Config mConfig;
    private Global mGlobal;

    public static final String BASEURL = "https://rtc.wsstreet.net";
    public static final String JOIN = BASEURL+"/api/auth/join";
    public static final String CANCEL = BASEURL+ "/api/cancle";
    public static final String WAIT =  BASEURL+"/api/wait";
    public static final String COMMENT =  BASEURL+"/api/comment";
    public static final String SENDMSG = BASEURL+ "/api/rtm";


    @Override
    public void onCreate() {
        super.onCreate();
        init();
        Utils.init(this);
        OkGo.getInstance().init(this);
        LogUtils.getConfig().setLogSwitch(true);
        LogUtils.getConfig().setGlobalTag("openduo");
        CrashReport.initCrashReport(this, "56549ac3e9", true);

    }

    private void init() {
        initConfig();
        initEngine();
    }

    private void initConfig() {
        mConfig = new Config();
        mGlobal = new Global();
    }

    private void initEngine() {
        String appId = getString(R.string.private_app_id);
        if (TextUtils.isEmpty(appId)) {
            throw new RuntimeException("请配置appid");
        }

        mEventListener = new EngineEventListener();
        try {
            mRtcEngine = RtcEngine.create(getApplicationContext(), appId, mEventListener);
            mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);//设置为直播场景
            mRtcEngine.enableDualStreamMode(true);
            mRtcEngine.enableVideo();
            mRtcEngine.enableWebSdkInteroperability(true);
            mRtcEngine.setLogFile(FileUtil.rtmLogFile(getApplicationContext()));

            mRtmClient = RtmClient.createInstance(getApplicationContext(), appId, mEventListener);
            mRtmClient.setLogFile(FileUtil.rtmLogFile(getApplicationContext()));

            if (Config.DEBUG) {
                mRtcEngine.setParameters("{\"rtc.log_filter\":65535}");
                mRtmClient.setParameters("{\"rtm.log_filter\":65535}");
            }
            rtmCallManager = mRtmClient.getRtmCallManager();
            rtmCallManager.setEventListener(mEventListener);

            // By default do not use rtm token
//            mRtmClient.login(null, mConfig.getUserId(), new ResultCallback<Void>() {
//                @Override
//                public void onSuccess(Void aVoid) {
//                    Log.i(TAG, "rtm client login success");
//                }
//
//                @Override
//                public void onFailure(ErrorInfo errorInfo) {
//                    Log.i(TAG, "rtm client login failed:" + errorInfo.getErrorDescription());
//                }
//            });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public RtcEngine rtcEngine() {
        return mRtcEngine;
    }

    public RtmClient rtmClient() {
        return mRtmClient;
    }

    public void registerEventListener(IEventListener listener) {
        mEventListener.registerEventListener(listener);
    }

    public void removeEventListener(IEventListener listener) {
        mEventListener.removeEventListener(listener);

    }

    public RtmCallManager rtmCallManager() {
        return rtmCallManager;
    }

    public Config config() {
        return mConfig;
    }

    public Global global() {
        return mGlobal;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        destroyEngine();
    }



    private void destroyEngine() {
        RtcEngine.destroy();

        mRtmClient.logout(new ResultCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.i(TAG, "rtm client logout success");
            }

            @Override
            public void onFailure(ErrorInfo errorInfo) {
                Log.i(TAG, "rtm client logout failed:" + errorInfo.getErrorDescription());
            }
        });
    }
}
